package com.atguigu.flink.window;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created by Smexy on 2023/1/30
 *
 *
 *      基于元素个数的滑动窗口，推荐使用process，因为无法做到滚动聚合！
 *          s1,1,1
 *          s1,1,2
 *          s1,1,3
 *          [1,2,3]
 *              触发运算时，两两运算。
 *           [1,3,6,4,5,6]
 *
 *
 */
public class Demo11_Special
{
    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 3333);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

                env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .keyBy(WaterSensor::getId)
                   .countWindow(5,3)
                   .reduce(new ReduceFunction<WaterSensor>()
                   {
                       @Override
                       public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                           System.out.println("Demo7_Reduce.reduce");
                           value2.setVc(value1.getVc() + value2.getVc());
                           return value2;
                       }
                   })
                   .print();

        
                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
        
    }
}
